package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.daoman.Floor;
import com.example.daoman.Trade;
import com.example.daoman.User;
import com.example.mapper.TradeMapper;
import com.example.mapper.UserMapper;
import com.example.service.TradeService;
import com.example.utils.GetSession;
import com.example.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TradeServiceImpl extends ServiceImpl<TradeMapper, Trade> implements TradeService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TradeMapper tradeMapper;

    @Autowired
    private TradeServiceImpl tradeService;

    //    管理员删除该换寝室申请
    @Override
    public Message removeAssess(Trade trade) {
        Map<String, Object> map = new HashMap<>();
        map.put("remove", "1");
        map.put("id", trade.getId());
        map.put("statue", "已取消");
        map.put("note", trade.getNote());
        boolean b = tradeMapper.removeOrUpdateAssess(map);
        return Message.out(200, "删除操作", b);
    }

    //    管理员受理换寝室申请
    @Override
    public Message setAssess(Trade trade) {
        Map<String, Object> map = new HashMap<>();
        map.put("update", "1");
        map.put("id", trade.getId());
        map.put("statue", "已受理");
        boolean b = tradeMapper.removeOrUpdateAssess(map);
//        两个学员之间交换床位信息
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", trade.getUser().getId());
        map1.put("bid", trade.getUser().getBeds().getId());
        map1.put("id1", trade.getUser1().getId());
        map1.put("bid1", trade.getUser1().getBeds().getId());
        boolean b1 = tradeMapper.changeBeds(map1);
        return Message.out(200, "受理操作", b && b1);
    }


    //    管理员查询所有的换寝室申请记录消息

    @Override
    public Message getMyAllFloorAssess(List<Floor> floorList) {
        Map<String,Object> map = new HashMap<>();
        ArrayList<Integer> arrayList= new ArrayList<>();
        for (Floor f:floorList){
            arrayList.add(f.getId());
        }
        map.put("fid",arrayList);
        List<Trade> floorAllAssess = tradeMapper.getFloorAllAssess(map);
        return Message.out(200, "查询所有", floorAllAssess);
    }

    //    学员查看自己的换寝室申请
    @Override
    public Message getMyChangeTrade() {
        try {
            int id = GetSession.get().getId();
//            查询自己申请的
            Map<String, Object> map = new HashMap<>();
            map.put("uid", id);
            map.put("my", "my");
            List<Trade> tradeList = tradeMapper.getMyChangeRoom(map);
//            查询被申请的记录
            Map<String, Object> map1 = new HashMap<>();
            map1.put("uid", id);
            map1.put("you", id);
            List<Trade> tradeList1 = tradeMapper.getMyChangeRoom(map1);
//            数据整合
            Map<String, Object> back = new HashMap<>();
            back.put("accord", tradeList);
            back.put("passive", tradeList1);
            return Message.out(200, "查询", back);
        } catch (Exception e) {
            return null;
        }
    }

    //    添加学员换晴是申请
    @Override
    public Message addTrade(User user) {
        try {
            int id = GetSession.get().getId();
//            判断是否已经存在了该申请
            QueryWrapper<Trade> q = new QueryWrapper<>();
            QueryWrapper<Trade> eq = q.eq("accord", id).eq("passive", user.getId());
            List<Trade> list = tradeService.list(eq);
            if (list.size() == 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("accord", id);
                map.put("note", user.getNote());
                map.put("passive", user.getId());
                map.put("statue", "申请中");
                boolean b = tradeMapper.addTrade(map);
                return Message.out(200, "添加", b);
            } else {
                return Message.out(200, "该申请已存在", false);
            }
        } catch (Exception e) {
            return null;
        }

    }

    //    搜索功能
    @Override
    public Message inputTrade(User user) {
        try {
            Map<String, Object> map = new HashMap<>();
            System.out.println(user);
            map.put("fid", user.getId());
            map.put("uid", GetSession.get().getId());
            map.put("account", user.getAccount());
            List<User> floorAllUser = userMapper.getFloorAllUser(map);
            return Message.out(200, "搜索", floorAllUser);
        } catch (Exception e) {
            return null;
        }
    }

    //    学员换寝室申请 查询你自己楼层中的所有的学员信息，分页查询
    @Override
    public Message getMyUserAll(int page, int pageSize, int fid) {
        try {
            int uid = GetSession.get().getId();
            Map<String, Object> map = new HashMap<>();
            map.put("fid", fid);
            map.put("uid", uid);
            map.put("page", (page - 1) * pageSize);
            map.put("pageSize", pageSize * page);
            List<User> floorAllUser = userMapper.getFloorAllUser(map);
//            查询楼层人数
            Map<String, Object> m = new HashMap<>();
            ArrayList<Integer> arrayList = new ArrayList<>();
            arrayList.add(fid);
            m.put("fidList", arrayList);
            int tradeNumber = tradeMapper.getTradeNumber(m);
//            数据返回
            Map<String, Object> message = new HashMap<>();
            message.put("total", tradeNumber);
            message.put("info", floorAllUser);
            return Message.out(200, "查询", message);
        } catch (Exception e) {
            return null;
        }

    }
}
